#pragma once
#include "Log.hpp"
#include "Command.hpp"
#include "InetAddr.hpp"
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>

//类模板方法
namespace SocketModule
{
    using namespace LogModule;
    static int gbacklog=16;
    class Socket
    {
    public:
        virtual ~Socket() {};
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual int Accept(InetAddr* client) = 0;
        virtual int Recv(std::string* out)=0;
        virtual int Send(std::string& in)=0;
        virtual void Close() =0 ;
        virtual int Connect(const std::string& serve_ip,uint16_t port)=0;
        virtual int Fd()=0;

    public:
        void BulidListenSocketMethod(uint16_t port,int backlog=gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
        void BulidTcpClienttMethod()
        {
            SocketOrDie();
        }
        // void BulidUdpSocketMethod()
        // {
        //     SocketOrDie();
        //     BindOrDie();
        // }

    private:
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(){}
        TcpSocket(int fd)
        :_sockid(fd)
        {}
        ~TcpSocket() {};
        void SocketOrDie() override
        {
            _sockid = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockid < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCK_ERR);
            }
            LOG(LogLevel::INFO) << "socket success";
        };

        void BindOrDie(uint16_t port) override
        {
            InetAddr addr(port);
            int n = ::bind(_sockid, addr.NetAddrPtr(), addr.NetLen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind success";
        };

        void ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockid, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error";
                exit(LISTEN_ERR);
            }
            LOG(LogLevel::INFO) << "listen success";
        };

        //服务端
        int Accept(InetAddr* client) override
        {
            struct sockaddr_in peer;
            socklen_t len=sizeof(peer);
            int fd=::accept(_sockid,CONV(peer),&len);
            if(fd<0)
            {
                LOG(LogLevel::FATAL)<<"accept error";
                return -1;
            }
            client->SetAddr(peer);
            return fd;
             
            // return std::make_shared<TcpSocket>(fd);
        };

        //客户端
        int Connect(const std::string& serve_ip,uint16_t port) override
        {
            InetAddr addr(serve_ip,port);
            return ::connect(_sockid,addr.NetAddrPtr(),addr.Len());
        }   

        int Recv(std::string* out) override
        {
            char buffer[4096*2];
            int n=::recv(_sockid,buffer,sizeof(buffer)-1,0);
            if(n>0)
            {
                buffer[n]=0;
                *out+=buffer;
            }
            return n;
        }

        int Send(std::string& in) override
        {
            int n=::send(_sockid,in.c_str(),in.size(),0);
            return n;
        }
        void Close() override
        {
            if(_sockid>0)
            {
                close(_sockid);
            }
        }


        int Fd()
        {
            return _sockid;
        }
    private:
        int _sockid;
    };









    // class UdpSocket : public Socket
    // {
    //     ~UdpSocket() {};
    //     void SocketOrDie() override {
    //     };
    //     void BindOrDie() {

    //     };
    //     void ListenOrDie() {

    //     };
    //     bool Accept() {
           
    //     };
    // };
};